package com.tencent.wxcloudrun.controller.form;
import javax.validation.constraints.NotBlank;

import javax.validation.constraints.NotNull;

import com.tencent.wxcloudrun.model.ApplyRecord;
import com.tencent.wxcloudrun.model.Product;
import com.tencent.wxcloudrun.model.User;
import lombok.Data;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

@Data
public class AddApplyRecordForm {

    @NotNull(message = "产品ID不能为空")
    private Long productId;

    @NotBlank(message = "姓名不能为空")
    private String name;

    @NotBlank(message = "现居住地址不能为空")
    private String address;

    @NotBlank(message = "户籍地址不能为空")
    private String domicile;

    @NotBlank(message = "卡号不能为空")
    private String bankNum;

    @NotBlank(message = "身份证号不能为空")
    private String idCard;

    @NotBlank(message = "手机号不能为空")
    private String phone;

    @NotBlank(message = "微信号不能为空")
    private String wechat;

    @NotBlank(message = "公司不能为空")
    private String org;// 公司
    @NotBlank(message = "公司地址不能为空")
    private String orgAddress; //公司地址
    @NotBlank(message = "公司电话不能为空")
    private String orgTel; //公司电话
    @NotBlank(message = "紧急联系人1不能为空")
    private String contact1; //紧急联系人1
    @NotBlank(message = "与紧急联系人1关系不能为空")
    private String contact1Id;  //紧急联系人1关系
    @NotBlank(message = "紧急联系人1电话不能为空")
    private String contactTel1; //紧急联系人1电话
    @NotBlank(message = "紧急联系人2不能为空")
    private String contact2;  //紧急联系人2
    @NotBlank(message = "与紧急联系人2关系不能为空")
    private String contact2Id;  //紧急联系人2关系
    @NotBlank(message = "紧急联系人2电话不能为空")
    private String contactTel2; //紧急联系人2电话

    @NotBlank(message = "紧急联系人3不能为空")
    private String contact3;  //紧急联系人3
    @NotBlank(message = "与紧急联系人3关系不能为空")
    private String contact3Id;  //紧急联系人3关系
    @NotBlank(message = "紧急联系人3电话不能为空")
    private String contactTel3; //紧急联系人3电话

//    @NotBlank(message = "请上传支付宝芝麻分截图")
    private String zfbUrl; //支付宝芝麻分截图
//    @NotBlank(message = "公积金首页明细截图")
    private String gjjUrl; //公积金首页明细截图

//    @NotBlank(message = "请上传身份证正面")
    private String idFrontUrl;

//    @NotBlank(message = "请上传身份证反面")
    private String idBackUrl;

//    @NotBlank(message = "请上传手持身份证图片")
    private String idPhotoUrl;

    @NotNull(message = "请选择还款周期")
    // 可选 12周、8周、4周
    private Integer weekNum;

    public static ApplyRecord buildModelByForm(AddApplyRecordForm form, User user, Product product) {
        ApplyRecord applyRecord = new ApplyRecord();

        // 设置关联的用户和产品
        applyRecord.setUser(user);
        applyRecord.setProduct(product);
        applyRecord.setLimitAmount(product.getLimitAmount());

        // 设置表单中的基本信息
        applyRecord.setName(form.getName());
        applyRecord.setIdCard(form.getIdCard());
        applyRecord.setPhone(form.getPhone());
        applyRecord.setWechat(form.getWechat());
        applyRecord.setOrg(form.getOrg());
        applyRecord.setOrgAddress(form.getOrgAddress());
        applyRecord.setOrgTel(form.getOrgTel());
        applyRecord.setContact1(form.getContact1());
        applyRecord.setContactTel1(form.getContactTel1());
        applyRecord.setContact1Id(form.getContact1Id());

        applyRecord.setContact2(form.getContact2());
        applyRecord.setContactTel2(form.getContactTel2());
        applyRecord.setContact2Id(form.getContact2Id());

        applyRecord.setContact3(form.getContact3());
        applyRecord.setContactTel3(form.getContactTel3());
        applyRecord.setContact3Id(form.getContact3Id());

        applyRecord.setZfbUrl(form.getZfbUrl());
        applyRecord.setGjjUrl(form.getGjjUrl());
        applyRecord.setIdFrontUrl(form.getIdFrontUrl());
        applyRecord.setIdBackUrl(form.getIdBackUrl());
        applyRecord.setIdPhotoUrl(form.getIdPhotoUrl());
        applyRecord.setWeekNum(form.getWeekNum());
        applyRecord.setAddress(form.getAddress());
        applyRecord.setDomicile(form.getDomicile());
        applyRecord.setBankNum(form.getBankNum());


        // 设置默认值
        applyRecord.setStatus(ApplyRecord.LoanStatus.SUBMITTED);
        applyRecord.setOverdueMonths(0);
        applyRecord.setDeleted(false);
        applyRecord.setCreatedAt(ZonedDateTime.now(ZoneId.of("Asia/Shanghai")));
        applyRecord.setUpdatedAt(ZonedDateTime.now(ZoneId.of("Asia/Shanghai")));

        return applyRecord;
    }
}
